import * as THREE from "three";

// 目标： 门的3D效果

// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

// 1.创建场景
const scene = new THREE.Scene();
// 2. 创建相机
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// 设置相机位置
camera.position.set(0, 0, 10);
// 添加相机到场景中
scene.add(camera);

const event = {};
event.onload = function () {
  console.log("图片加载完成")
}
event.onProgress = function (url, num, total) {
  console.log("url", url);
  console.log("num", num);
  console.log("total", total);
  console.log("图片加载进度");
}
event.onError = function (e) {
  console.log(e);
  console.log("图片加载出现错误");
}

// 设置加载管理器
const loadingManager = new THREE.LoadingManager(
  event.onload, event.onProgress, event.onError, 
)

// 导入纹理
const textureLoader = new THREE.TextureLoader(loadingManager);
const floorColor = textureLoader.load(require("../assets/img/door/color.jpg"));
// 添加黑则隐白则显的纹理用于透明化非主体部分
const doorAplhaColor = textureLoader.load(
  require("../assets/img/door/alpha.jpg")
);
// 导入AO环境遮挡贴图
const doorAOTexture = textureLoader.load(
  require("../assets/img/door/ambientOcclusion.jpg")
);
// 导入置换贴图
const doorHightTexture = textureLoader.load(
  require("../assets/img/door/height.jpg")
);
// 导入粗糙度贴图
const ronghnessTexture = textureLoader.load(
  require("../assets/img/door/roughness.jpg")
);
// 导入金属贴图
const metalnessTexture = textureLoader.load(
  require("../assets/img/door/metalness.jpg")
);
// 导入法线贴图
const normalTexture = textureLoader.load(
  require("../assets/img/door/normal.jpg")
);

// 3. 创建几何体
const geometry = new THREE.BoxGeometry(1, 1, 1, 100, 100, 100);

// 给物体添加第二组uv
geometry.setAttribute(
  "uv2",
  new THREE.BufferAttribute(geometry.attributes.uv.array, 2)
);
// 材质
const material = new THREE.MeshStandardMaterial({
  // color: 0x00ff00,
  map: floorColor,
  alphaMap: doorAplhaColor,
  transparent: true,
  aoMap: doorAOTexture,
  aoMapIntensity: 1,
  displacementMap: doorHightTexture, // 加载置换贴图
  displacementScale: 0.1,
  roughness: 1, // 粗糙度
  roughnessMap: ronghnessTexture, // 加载粗糙贴图
  metalness: 1, // 金属度
  metalnessMap: metalnessTexture, // 加载金属贴图
  normalMap: normalTexture // 加载法线贴图
});

// 关联几何体和材质
const cube = new THREE.Mesh(geometry, material);

// 添加平面
const planeGeometry = new THREE.PlaneGeometry(1, 1, 200, 200);
const plane = new THREE.Mesh(planeGeometry, material);
plane.position.set(1.5, 0, 0);
scene.add(plane);

// 修改几何体位置
cube.position.set(0, 0, 0);


// 添加几何体到场景
scene.add(cube);

// 灯光
// 添加环境光
const light = new THREE.AmbientLight(0x404040); // soft white light

scene.add(light);

// 直线光源(平行光)
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
// 设置直线光源的照射位置
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);

// 4. 初始化渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

function animate() {
  controls.update();
  renderer.render(scene, camera);
  // 渲染下一帧的时候就调用animate
  requestAnimationFrame(animate);
}
animate();

// 监听画面变化
window.addEventListener("resize", () => {
  const iwidth = window.innerWidth;
  const iheigt = window.innerHeight;
  //更新摄像头
  camera.aspect = iwidth / iheigt;
  // 更新摄像机投影矩阵
  camera.updateProjectionMatrix();
  // 更新渲染器
  renderer.setSize(iwidth, iheigt);
  // 设置渲染器像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});
